//
//  SleepChartView.m
//  project
//
//  Created by 黄建华 on 2019/1/10.
//  Copyright © 2019年 黄建华. All rights reserved.
//

#import "SleepChartView.h"

@implementation SleepChartView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
    
    }
    return self;
}

- (void)createView
{
    
}
//#define SNSLEEPCOLORDEEP  [UIColor colorFromHex:0x6d4ca2]
//#define SNSLEEPCOLORLIGHT [UIColor colorFromHex:0x8276c8]
//#define SNSLEEPCOLORSOBER [UIColor colorFromHex:0xb6bbe4]
- (void)updateWith:(NSInteger)current aveValue:(NSInteger)value withType:(NSInteger)type
{
    NSArray *array = [NSArray arrayWithObjects:SNSLEEPCOLORDEEP, SNSLEEPCOLORLIGHT, SNSLEEPCOLORSOBER, nil];
    
    [self removeAllSubviews];
    CGFloat width = self.width;
    
    CGFloat valueWidth = 0.9*width*current/100.0;
    CGFloat adth = 0.1 *width;
    
    CGFloat valueWidth2 = 0.9*width*value/100.0;

    UIView *viewAve = [[UIView alloc] init];
    viewAve.frame = SNFRAME(0, 0,adth + valueWidth2, self.height);
    viewAve.backgroundColor = [array objectAtIndex:type];
    viewAve.alpha = 0.5;
    [self addSubview:viewAve];
    
    UIView *view1 = [[UIView alloc] init];
    view1.frame = SNFRAME(0, 0,valueWidth+adth, self.height);
    view1.backgroundColor = [array objectAtIndex:type];
    [self addSubview:view1];
    
    CGFloat right = 0.0;
    if (current <= value) {
        right = viewAve.right;
    }
    else
    {
        right = view1.right;
    }
    
    DrawView *dog = [[DrawView alloc] initWithFrame:SNFRAME(right-3, viewAve.top -3, 4, viewAve.height+3)];
    [self addSubview:dog];
    
    UILabel *value1 = [UILabel simpleWithRect:SNFRAME(0,view1.height- 18, view1.width, 15) withAlignment:NSTextAlignmentCenter withFontSize:10 withText:[NSString stringWithFormat:@" %d%%",current] withTextColor:[UIColor blackColor]];
    value1.adjustsFontSizeToFitWidth = YES;
    [view1 addSubview:value1];
    
    UILabel *value2 = [UILabel simpleWithRect:SNFRAME(right + 10,view1.top +5,130,view1.height -10) withAlignment:NSTextAlignmentLeft withFontSize:10 withText:@"" withTextColor:[UIColor blackColor]];
    value2.adjustsFontSizeToFitWidth = YES;
    value2.numberOfLines = 2;
    [self addSubview:value2];
    
    NSString *info = @"";
    if (type == 0) {
        info = SN_Text(@"深睡");
    }
    else if(type == 1)
    {
        info = SN_Text(@"浅睡");
    }
    else
    {
        info = SN_Text(@"清醒");
    }
    value2.text = [NSString stringWithFormat:@"%@\n%d%%",info,value];
}

@end
